package com.ambercrm.business;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.ambercrm.R;
import com.ambercrm.base.BaseActivity;
import com.ambercrm.http.HttpUtils;
import com.ambercrm.tools.FileUtils;
import com.ambercrm.tools.LogUtils;
import com.ambercrm.tools.UpGradeUtils;

public class UpGradeActivity extends BaseActivity {

//    String mVersionName;
    String mApkUrl;
    String apkName = "name.apk";  // 改成自己的apk名字
    FileUtils mFileUtils;
    boolean isDownloading = false;
    Dialog mDialog;
    int isForceUpdate = 0; // 0 非强制， 1强制
    String mMark;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFileUtils = new FileUtils();
        Bundle b = getIntent().getExtras();

        if (b != null) {
//            mVersionName = b.getString("versionName");
            mApkUrl = b.getString("apkUrl");
            isForceUpdate = b.getInt("isForce");
            mMark = b.getString("mark");
            if (mApkUrl != null) {
                View v = LayoutInflater.from(this).inflate(R.layout.upgrade_dialog_bg_layout, null);
                final ProgressBar progressBar = v.findViewById(R.id.progress_bar);
                final TextView contentText = v.findViewById(R.id.new_version_content);
                contentText.setText("新版本：" + mMark);
                final Button upgradeBt = v.findViewById(R.id.upgrade_bt);

                upgradeBt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (isDownloading) {
                            isDownloading = false;
                            upgradeBt.setText("继续");
                        }
                        else {
                            isDownloading = true;
                            upgradeBt.setText("暂停");
                        }

                        HttpUtils.downloadOneFile(mApkUrl, new HttpUtils.OnDownloadFileResponseListener() {
                            @Override
                            public void onStart() {
//                                progressBar.setVisibility(View.VISIBLE);
//                              contentText.setVisibility(View.GONE);
                                mDialog.dismiss();
                                showProgressDialog(mContext, "下载中...");
                            }

                            @Override
                            public void onFailed(String result) {

                            }

                            @Override
                            public void onFinish(String path) {

                            }

                            @Override
                            public void onProgress(int progress) {
                                LogUtils.showLog("progress:" + progress);
                                progressBar.setProgress(progress);
                                if (progress == 100) {
                                    UpGradeUtils.startInstallApk(UpGradeActivity.this, apkName);
                                    dismissProgressDialog();
                                    finish();
                                }
                            }
                        }, mFileUtils.getApkPath().getAbsolutePath(), apkName);
                    }
                });

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setView(v);
                mDialog = builder.create();

                if (isForceUpdate == 0) {
                    mDialog.setCanceledOnTouchOutside(true);
                    mDialog.setCancelable(true);
                }
                else {
                    mDialog.setCanceledOnTouchOutside(false);
                    mDialog.setCancelable(false);
                }
                mDialog.show();
                mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (!isDownloading) {
                            finish();
                        }
                    }
                });
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        UpGradeUtils.startInstallApk(this, apkName);
    }
}
